﻿Imports System.Xml
Imports System.IO

Public Class settings

    Public Sub LoadSettings(ByVal path As String)
        Dim doc As New XmlDocument

        doc.Load(path)

        Dim root As XmlNode = doc.SelectSingleNode("settings")

        Dim loc As XmlNode = root.SelectSingleNode("locale")
        cmb_locale.Text = loc.InnerText

        Dim dayc As XmlNode = root.SelectSingleNode("dayint")
        txt_daycount.Text = dayc.InnerText

        Dim usem As XmlNode = root.SelectSingleNode("metric")
        cmb_units.Text = usem.InnerText

        Dim updateint As XmlNode = root.SelectSingleNode("update-interval")
        txt_update.Text = updateint.InnerText

    End Sub

    Public Sub SaveSettings(ByVal path As String)
        Dim doc As New XmlDocument

        doc.Load(path)

        Dim root As XmlNode = doc.SelectSingleNode("settings")

        Dim loc As XmlNode = root.SelectSingleNode("locale")
        loc.InnerText = cmb_locale.Text

        Dim dayc As XmlNode = root.SelectSingleNode("dayint")
        dayc.InnerText = txt_daycount.Text

        Dim usem As XmlNode = root.SelectSingleNode("metric")
        usem.InnerText = cmb_units.Text

        Dim updateint As XmlNode = root.SelectSingleNode("update-interval")
        updateint.InnerText = txt_update.Text

        doc.Save(path)
    End Sub

    Private Sub settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If File.Exists(Application.StartupPath & "\settings.xml") Then
            LoadSettings(Application.StartupPath & "\settings.xml")
        End If
    End Sub

    Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
        If File.Exists(Application.StartupPath & "\settings.xml") Then
            SaveSettings(Application.StartupPath & "\settings.xml")
            Application.Restart()
        End If
    End Sub

    Private Sub btn_cancel_Click(sender As Object, e As EventArgs) Handles btn_cancel.Click
        Me.Close()
    End Sub
End Class